<template>
	<view class="container">
		<!-- 服务类型网格 -->
		<view class="service-grid">
			<view 
				class="grid-item" 
				v-for="(item, index) in serviceTypes" 
				:key="index"
				@tap="selectServiceType(item.type)"
				:class="{ active: currentType === item.type }"
			>
				<text>{{item.name}}</text>
			</view>
		</view>

		<!-- 订单列表 -->
		<scroll-view 
			class="order-scroll" 
			scroll-y 
			@scrolltolower="loadMore"
		>
			<view class="order-list">
				<view class="order-item" v-for="(item, index) in filteredOrders" :key="index" @tap="goToDetail(item)">
					<view class="order-header">
						<view class="user-info">
							<image class="avatar" src="/static/avatar.png" mode="aspectFill"></image>
							<text class="nickname">{{item.userName}}</text>
						</view>
						<text class="order-tag">{{item.type}}</text>
					</view>
					<view class="order-content">
						<text class="order-title">{{item.title}}</text>
						<text class="order-desc">{{item.description}}</text>
						<view class="order-footer">
							<text class="price">￥{{item.price}}</text>
							<view class="info">
								<text class="location">{{item.location}}</text>
								<text class="time">{{item.time || '尽快送达'}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="loading" v-if="isLoading">
				<uni-load-more status="loading" :content-text="loadMoreText"></uni-load-more>
			</view>
			<view class="empty" v-if="filteredOrders.length === 0">
				<text>暂无相关订单</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			currentType: 'all',
			isLoading: false,
			page: 1,
			loadMoreText: {
				contentdown: '上拉加载更多',
				contentrefresh: '加载中...',
				contentnomore: '没有更多数据了'
			},
			serviceTypes: [
				{ type: 'all', name: '全部' },
				{ type: 'takeout', name: '取外卖' },
				{ type: 'express', name: '取快递' },
				{ type: 'drink', name: '买奶茶' },
				{ type: 'fruit', name: '买水果' },
				{ type: 'food', name: '买饭' },
				{ type: 'print', name: '送文件' },
				{ type: 'flower', name: '送鲜花' },
				{ type: 'goods', name: '送礼物' },
				{ type: 'movie', name: '陪看电影' },
				{ type: 'shopping', name: '陪逛街' },
				{ type: 'game', name: '游戏陪练' }
			],
			orderList: [
				{
					id: 1,
					userName: '张三',
					type: '取快递',
					title: '帮取快递',
					description: '菜鸟驿站有一个快递，求帮忙取一下',
					price: 5,
					location: '菜鸟驿站',
					time: '12:30前',
					status: '待接单',
					contact: '13800138000',
					createTime: '2024-03-20 10:30'
				},
				{
					id: 2,
					userName: '李四',
					type: '买饭',
					title: '帮买午饭',
					description: '一食堂二楼炒饭，要辣椒',
					price: 8,
					location: '一食堂',
					time: '11:30前',
					status: '待接单',
					contact: '13800138001',
					createTime: '2024-03-20 10:00'
				},
				{
					id: 3,
					userName: '王五',
					type: '打印',
					title: '打印课程作业',
					description: '需要打印10页A4纸，双面打印',
					price: 3,
					location: '图书馆',
					time: '14:00前',
					status: '待接单',
					contact: '13800138002',
					createTime: '2024-03-20 09:30'
				}
			]
		}
	},
	computed: {
		filteredOrders() {
			if (this.currentType === 'all') {
				return this.orderList
			}
			return this.orderList.filter(order => {
				const typeMap = {
					'takeout': '取外卖',
					'express': '取快递',
					'drink': '买奶茶',
					'fruit': '买水果',
					'food': '买饭',
					'print': '送文件',
					'flower': '送鲜花',
					'goods': '送礼物',
					'movie': '陪看电影',
					'shopping': '陪逛街',
					'game': '游戏陪练'
				}
				return order.type === typeMap[this.currentType]
			})
		}
	},
	methods: {
		selectServiceType(type) {
			this.currentType = type
		},
		goToDetail(item) {
			const orderInfo = encodeURIComponent(JSON.stringify(item))
			uni.navigateTo({
				url: `/pages/order-detail/index?orderInfo=${orderInfo}`
			})
		},
		async loadMore() {
			if (this.isLoading) return
			this.isLoading = true
			
			// TODO: 调用后端API获取更多数据
			// 模拟加载更多数据
			await new Promise(resolve => setTimeout(resolve, 1000))
			
			// 模拟新数据
			const newOrders = [
				{
					id: this.orderList.length + 1,
					userName: '新用户',
					type: '取快递',
					title: '新的快递订单',
					description: '这是加载更多获取的新订单',
					price: 6,
					location: '快递点',
					time: '15:30前',
					status: '待接单',
					contact: '13800138003',
					createTime: '2024-03-20 11:30'
				}
			]
			
			this.orderList = [...this.orderList, ...newOrders]
			this.page++
			this.isLoading = false
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	display: flex;
	flex-direction: column;
}

.service-grid {
	background-color: #fff;
	padding: 20rpx;
	display: flex;
	flex-wrap: wrap;
	
	.grid-item {
		width: 25%;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		
		text {
			font-size: 28rpx;
			color: $text-color;
			padding: 16rpx 24rpx;
			border-radius: 30rpx;
			background-color: #f8f8f8;
		}
		
		&.active {
			text {
				color: #2979ff;
				background-color: rgba(#2979ff, 0.1);
			}
		}
	}
}

.order-scroll {
	flex: 1;
}

.order-list {
	padding: 20rpx;
	
	.order-item {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		
		.order-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.user-info {
				display: flex;
				align-items: center;
				
				.avatar {
					width: 60rpx;
					height: 60rpx;
					border-radius: 30rpx;
					margin-right: 10rpx;
				}
				
				.nickname {
					font-size: 28rpx;
					color: $text-color;
				}
			}
			
			.order-tag {
				font-size: 24rpx;
				color: #2979ff;
				background-color: rgba(#2979ff, 0.1);
				padding: 4rpx 12rpx;
				border-radius: 4rpx;
			}
		}
		
		.order-content {
			.order-title {
				font-size: 32rpx;
				color: $text-color;
				margin-bottom: 10rpx;
				display: block;
			}
			
			.order-desc {
				font-size: 28rpx;
				color: $text-color-grey;
				margin-bottom: 20rpx;
				display: block;
			}
			
			.order-footer {
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.price {
					color: #ff5500;
					font-size: 36rpx;
					font-weight: bold;
				}
				
				.info {
					display: flex;
					align-items: center;
					
					.location, .time {
						font-size: 24rpx;
						color: $text-color-grey;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
}

.loading {
	padding: 20rpx;
	text-align: center;
}

.empty {
	padding: 100rpx 0;
	text-align: center;
	color: $text-color-grey;
	font-size: 28rpx;
}
</style> 